home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 001-100 / 001-025 / 004 / bm / bm.c < prev    next >
C/C++ Source or Header  |  1995-03-17  |  2KB  |  105 lines

  1. #include <stdio.h>
  2.  
  3. #ifdef BSD
  4. #include <sys/file.h>
  5. #else
  6. #include <fcntl.h>
  7. #endif
  8.  
  9. #ifdef BSD
  10. #include <strings.h>
  11. #else
  12. #include <string.h>
  13. #endif
  14.  
  15. #include "bm.h"
  16. #include "Extern.h"
  17.  
  18. main(argc,argv)
  19. int argc;
  20. char *argv[];
  21. {
  22.     /* test driver for grep based on Boyer-Moore algorithm */
  23.     char BigBuff[MAXBUFF + 2];
  24.     /*
  25.     * We leave one extra character at the beginning and end of the buffer,
  26.     * but don't tell Execute about it. This is so when someone is
  27.     * scanning the buffer and scans past the end (or beginning)
  28.     * we are still technically in the buffer (picky, but there ARE
  29.     * machines which would complain)
  30.     */
  31.     int ret = 1, /* return code from Execute */
  32.         NotFound = 0,        /* non-zero if file not readable */
  33.         NFiles,
  34.         NPats; /* number of patterns to search for */
  35.     char i,
  36.         *FlagPtr,
  37.         **OptPtr; /* used to scan command line */
  38.     int TextFile /* file to search */;
  39.     struct PattDesc *DescVec[MAXPATS];
  40.  
  41.     --argc;
  42.     OptPtr = argv + 1;
  43.     while ( argc && **OptPtr == '-') {
  44.         FlagPtr = *OptPtr + 1;
  45.         while (*FlagPtr) {
  46.             switch (*FlagPtr) {
  47.                 case 'c': cFlag = 1; break;
  48.                 case 'e': eFlag = 1;
  49.                     /* get the patterns from next arg */
  50.                     NPats = MkDescVec(DescVec,*++OptPtr);
  51.                     --argc;
  52.                     break;
  53.                 case 'f': fFlag = 1; 
  54.                     /* read the patterns from a file */
  55.                     NPats = GetPatFile(*++OptPtr,DescVec);
  56.                     --argc;
  57.                     break;
  58.                 case 'l': lFlag = 1; break;
  59.                 case 'n': nFlag = 1; break;
  60.                 case 's': sFlag = 1; break;
  61.                 case 'x': xFlag = 1; break;
  62.                 case 'h': hFlag = 1; break;
  63.                 default:
  64.                     fprintf(stderr,
  65.                     "bm: invalid option: -%c \n",
  66.                     *FlagPtr);
  67.                     PutUsage();
  68.                     exit(2);
  69.             } /* switch */
  70.             ++FlagPtr;
  71.         } /* while */
  72.         ++OptPtr; --argc;
  73.     } /* while */
  74.     /* OptPtr now points to patterns */
  75.     if (!fFlag && !eFlag) {
  76.         if (!argc) {
  77.             fprintf(stderr,"bm: no pattern specified\n");
  78.             PutUsage();
  79.             exit(2);
  80.         } else
  81.             NPats = MkDescVec(DescVec,*OptPtr);
  82.         ++OptPtr; --argc;
  83.     }
  84.     /* OptPtr now points to first file */
  85.     NFiles = argc;
  86.     if (!NFiles)
  87.         ret &= Execute(DescVec,NPats,0,BigBuff+1);
  88.     else while (argc--) {
  89.         if ((NFiles > 1) || lFlag) FileName = *OptPtr;
  90.         if ((TextFile = open(*OptPtr,O_RDONLY,0)) < 0) {
  91.             fprintf(stderr,"bm: can't open %s\n",*OptPtr);
  92.             NotFound++;
  93.         }
  94.         else {
  95.             ret &= Execute(DescVec,NPats,TextFile,BigBuff+1);
  96.             if (sFlag && !ret)
  97.                 exit(0);
  98.             close(TextFile);
  99.         } /* if */
  100.         ++OptPtr;
  101.     } /* while */
  102.     if (cFlag) printf("%d\n",MatchCount);
  103.     exit(NotFound ? 2 : ret);
  104. } /* main */
  105.